<?php

namespace App\Repositories\Eloquent\Edu;

use Illuminate\Container\Container as App;
use App\Repositories\Eloquent\Repository;

use App\Repositories\Eloquent\Edu\TermRepository as Term;
use App\Repositories\Eloquent\Mgt\UserRepository as User;

class CategoryRepository extends Repository
{

    protected $user, $term;

    public function __construct(App $app, User $user, Term $term)
    {
        parent::__construct($app);

        $this->user = $user;

        $this->term = $term;
    }

    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Edu\Category';
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $result = $this->model
                ->with('clases','users')
                ->orderBy('id', 'desc');

        return $result;
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store($request, $termId)
    {
        $term = $this->term->find($termId);

        if (!$term) {
            return $this->responseFail(404,'培训计划信息不正确');
        }

        $result = $this->create([
            'type_id' => $term->type_id,
            'term_id' => $termId,
            'branch_id' => $term->branch_id,
            'name' => $request->category,
        ]);

        if( !!$result )
            return $this->responseSuccess('门类添加成功');

        return $this->responseFail(500, '门类添加失败');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $type = $this->find($id);

        if (!$type) {
            return $this->responseFail(404);
        }

        return $type;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function renew($request, $id)
    {
        $result = $this->update([
            'name' => $request->category,
        ], $id);

        if(!!$result)
            return $this->responseSuccess('门类修改成功');

        return $this->responseFail(500,'门类修改失败');
    }



    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


}